#!/usr/bin/env python
#coding=utf-8

#author: qiyongfeng

import json
import time
import sys
reload( sys )
sys.setdefaultencoding('utf-8')

def to_table(url, title, contents, tag, oriurl, orititle, zhaiyao=''):
    table=''
    if url == '':
        biaoti=title
    else:
        biaoti='<a href='+url+'>'+title+'</a>'
    table+='<div class="title">'+biaoti+zhaiyao+'</div>'
    table+='<div class="preview">'+contents+'</div>'
    table+='<div class="tag">'+tag+'</div>'
    if oriurl == '':
        size=orititle
    else:
        size='<a href='+oriurl+'>'+orititle+'</a>'
    table+='<div class="size">'+size+'</div>'
    table='<div class="item left">'+table+'</div>'
    return table

def main(filejson='starred.json',nstart=1,ncount=10):
    _head='''<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8";/>
<title>{0}</title>
<link type="text/css" rel="stylesheet" href="css/list.css" />
</head>
<body>
<div id="wrapper"><div id="head"><h2>{0}</h2><hr></div><div id="list">'''
    _tail='''</div><div id="footer"><hr>本文件由{0}生成 Copyright by ritow	<a href="#wrapper">[回到顶部]</a></div></div>
</body>
</html>'''
    #print _head
    #print _tail
    
    f=file(filejson)
    s=json.load(f)
    f.close()
    
    ff=open('output.html','w')
    ff.write(_head.format(s['title']))
    ff.write('\n')

    total=len(s["items"])
    if nstart < 0:
        nstart=total+nstart+1
    j=0
    end=nstart+ncount
    for i in s["items"]:
        j+=1
        
        if j<nstart: continue
        if j>=end: break
        try:
            title =  i["title"]
        except Exception as e:
            title = 'No Title'
        try:
            url = i["alternate"][0]["href"]
        except Exception as e:
            url = ''
        contents='ritow: (*^__^*)'
        try:
            contents=i["summary"]["content"]
            zhaiyao = '    [摘要]'
        except Exception as e:
            #continue
            pass
        try:
            contents=i["content"]["content"]
            zhaiyao = ''
        except Exception as e:
            pass
        pubtime=time.strftime('%Y-%m-%d %X', time.localtime(i["published"]))
        
        cate=''
        for k in i["categories"][2:]:
            if cate=='': cate='标签：'
            if k[:5] == 'user/': continue
            cate+=k+', '
        try:
            author='作者： '+i["author"] + '\t'
        except Exception as e:
            author=''
        tag="published: "+pubtime+'\t'+author+cate
        try:
            orititle=i["origin"]['title']
        except Exception as e:
            orititle = 'No Title'
        
        try:
            oriurl=i["origin"]["htmlUrl"]
        except Exception as e:
            oriurl=''
        ## print 
        #print to_table(url, title, contents, tag, oriurl, orititle)
        ff.write(to_table(url, title, contents, tag, oriurl, orititle, zhaiyao))
        ff.write('\n')
        print '%-8d%s' % (j,  title)

        
        
    ff.write(_tail.format(__file__))
    ff.write('\n')
    ff.close()


if __name__ == '__main__':
    argc=len(sys.argv)
    #print argc
    if argc >=2: rfjson=sys.argv[1]
    if argc >=3: rstart=int(sys.argv[2])
    if argc >=4: rcount=int(sys.argv[3])

    main(rfjson, rstart, rcount)

    
